package com.fzu.mmdbproxyframework.cluster;

import java.util.HashMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.fzu.mmdbproxyframework.cluster.passcloud.InternetwareCmd;
import com.fzu.mmdbproxyframework.cluster.passcloud.QueryAsyncJob;
import com.fzu.mmdbproxyframework.cluster.passcloud.SendRequest;

public class LB {
	public String createProject(HashMap<String, String> params) {
		String result = SendRequest.sendRequest(
				InternetwareCmd.CreateProjectCmd, params);
		if (null == result)
			return "fail";
		JSONObject obj = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.CreateProjectRes);
		if (null == obj)
			return "fail";
		JSONObject obj2 = QueryAsyncJob.sendAsynRequest(obj.getString("jobid"));
		if (obj2.getInt("jobstatus") == 1) {
			String projectId = obj2.getJSONObject("jobresult").getJSONObject(
			"project").getString("id");
				return projectId;
		}
		return "fail";
	}
	//HashMap<String, String> params = new HashMap<String, String>();
	//params.put("domainid", "0ef616bf-2927-4dfa-b4dc-d01a36667fa8");
	//params.put("account", "lzq");
	//params.put("displaytext", "Test");
	//params.put("name", "Test");
	//new ProjectOperation().createProject(params);--->PROJECT_ID
	public String createNetwork(HashMap<String, String> params){
		String result = SendRequest.sendRequest(
				InternetwareCmd.createNetworkCmd, params);
		if(null == result){
			return "fail";
		}
		JSONObject obj = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.createNetworkRes);
		if(null == obj || obj.has("errorcode")){
			return "fail";
		}
		return obj.getJSONObject("network").getString("id");
	}
	//HashMap<String, String> params = new HashMap<String, String>();
	//params.put("zoneid", "546f3fed-637d-467f-a46c-3556090fab7d");
	//params.put("name", "福州大学超级计算机中心私有网络");
	//params.put("displaytext", "福州大学超级计算机中心网络");
	//params.put("networkofferingid", "c760926f-f553-44e3-ae63-77eb8ada1563");
	//params.put("projectid", PROJECT_ID);
	//new VMNetworkOperation().createNetwork(params);--->NETWORKID//VM Creation
	//afterVMCreation 获取公共 IP 信息
	public String listPublicIps(
			HashMap<String, String> params) {
		String result = SendRequest.sendRequest(
				InternetwareCmd.ListPublicIpAddressesCmd, params);
		if (null == result) {
			return null;
		}
		JSONArray json = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.ListPublicIpAddressesRes).getJSONArray("publicipaddress");
		return json.getJSONObject(0).getString("id");
	}
	//HashMap<String, String> params = new HashMap<String, String>();
	//params.put("projectid", PROJECT_ID);
	//new PublicIpOperation().listPublicIps(params);--->PUBLICIPID
	/*
	 * //添加负载均衡规则
	 */
	public String createLoadBalancerRule(HashMap<String, String> params) {
		String result = SendRequest.sendRequest(
				InternetwareCmd.createLoadBalancerRuleCmd, params);
		if (null == result) {
			return "fail";
		}
		JSONObject createPortForwardingRuleRes = JSONObject.fromObject(result)
				.getJSONObject(InternetwareCmd.createLoadBalancerRuleRes);
		if (createPortForwardingRuleRes.isEmpty()
				|| createPortForwardingRuleRes.has("errorcode")) {
			return "fail";
		}
		JSONObject obj2 = QueryAsyncJob
				.sendAsynRequest(createPortForwardingRuleRes.getString("jobid"));
		if (obj2.getInt("jobstatus") == 1) {
			String firewallRuleId = obj2.getJSONObject("jobresult")
					.getJSONObject("loadbalancer").getString("id");
			return firewallRuleId;
		}
		return "fail";
	}
	//HashMap<String, String> params = new HashMap<String, String>();
	//params.put("algorithm", "leastconn");//source, roundrobin, leastconn
	//params.put("name", "LB");
	//params.put("privateport", "82");
	//params.put("publicport", "82");
	//params.put("publicipid", PUBLICIPID);
	//new LoadBalancerRuleOperation().createLoadBalancerRule(params);--->LBID
	/*
	 * 负载均衡应用到虚拟机
	 */
	public Boolean assignToLoadBalancerRule(HashMap<String, String> params) {
		Boolean success = false;
		String result = SendRequest.sendRequest(InternetwareCmd.assignToLoadBalancerRuleCmd, params);
		if(null == result){
			return success;
		}
		JSONObject obj = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.assignToLoadBalancerRuleRes);
		if(null == obj || obj.has("errorcode")){
			return success;
		}
		JSONObject obj2 = QueryAsyncJob.sendAsynRequest(obj.getString("jobid"));
		if (obj2.getInt("jobstatus") == 1) {
			return obj2.getJSONObject("jobresult").getBoolean("success");
		}
		return success;
	}
	//params.put("virtualmachineids", "742a6fc7-64ae-4a4e-a5a3-5432dece17f0,8f5c0c8f-4988-4364-a079-656c3ceba1ec");
	//params.put("id", LBID);
	//new LoadBalancerRuleOperation().assignToLoadBalancerRule(params);
	
	
}
